package com.ksana.context.test.bean;

import com.ksana.bean.factory.FactoryBean;
import com.ksana.context.test.mapper.UserMapper;
import com.ksana.util.ClassUtil;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

public class ProxyBeanFactory implements FactoryBean<UserMapper> {
    @Override
    public UserMapper getObject() {
        InvocationHandler handler = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Map<Integer, String> map = new HashMap<>();

                map.put(1, "fall");
                map.put(2, "fall");

                return "被代理了";
            }
        };
        return (UserMapper) Proxy.newProxyInstance(ClassUtil.getClassLoader(), new Class[]{UserMapper.class}, handler);
    }

    @Override
    public Class<?> getObjectType() {
        return UserMapper.class;
    }
}
